﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin_option_inc.aspx.cs" Inherits="newBlog.CenterInc.view_inc.admin.Admin_option_inc" %>
<%@import Namespace="System.IO" %>
<%@ import Namespace="Newtonsoft.Json.Linq" %>
<%@ import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<%  Response.ContentType = "application/json";
    if (Session["user_id"]==null) {
        Response.Write("{\"error\":\"您未登录本站\"}");
        return;
    }
    if (Convert.ToInt32(Session["user_grade"])!=1) { 
    Response.Write("{\"error\":\"您不是管理员\"}");
        return;
    }

    string title = Request.Form["title"]==null||Request.Form["title"].Length==0?"":Request.Form["title"];
    string key = Request.Form["key"]==null||Request.Form["key"].Length==0?"":Request.Form["key"];
    string content = Request.Form["content"]==null||Request.Form["content"].Length==0?"":Request.Form["content"];
    string notice1 = Request.Form["Notice1"]==null||Request.Form["Notice1"].Length==0?"":Request.Form["Notice1"];
    string notice2 = Request.Form["Notice2"]==null||Request.Form["Notice2"].Length==0?"":Request.Form["Notice2"];

    string json = "{\"title\":\""+title.Trim()+"\",\"key\":\""+key.Trim()+"\",\"content\":\""+content.Trim()+"\",\"notice1\":\""+notice1.Trim()+"\",\"notice2\":\""+notice2.Trim()+"\"}";
    string filepath = Server.MapPath("~/option.json");
    StreamWriter sw = new StreamWriter(filepath);
    try
    {
        
        sw.Write(json);

    }
    catch
    {
        Response.Write("{\"error\":\"出现错误写入失败\"}");
        return;
    }
    finally { 
    sw.Close();
    }
     using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                json = wc.DownloadString(filepath);
             
     }
            JObject model = JObject.Parse(json);
    Application.Clear();
    Application.Add("title",model["title"]);
    Application.Add("key", model["key"]);
    Application.Add("content", model["content"]);
    Application.Add("notice1", model["notice1"]);
    Application.Add("notice2", model["notice2"]);
    Response.Write("{\"msg\":\"写入成功\"}");
    %>
